from RequestToExtension.RequestHelper import post_request
from RequestToExtension.ExtensionConfig import *


class EasyRequestToExtension:
    def __init__(self, port):
        self._port = port
        self._base_url = EXTENSION_BASE_URL + str(port)

    def run_command_on_extension(self, command, parameters):
        post_data = {
            "command": command,
            "parameters": parameters
        }

        # 定义成功回调
        def on_success(return_response):
            print("Command run success:", return_response)

        # 定义错误回调
        def on_error(status_code, error_message):
            print("Command run error:", status_code, error_message)

        post_url = self._base_url + '/' + FUZZY_COMMAND_URL

        # 调用 post_request 并传递成功和错误回调
        post_request(post_url, post_data, on_success, on_error)

    def console_print_on_extension(self, data):
        post_data = {
            "data": data
        }

        def on_success(return_response):
            print("Console print success:", return_response)

        def on_error(status_code, error_message):
            print("Console print error:", status_code, error_message)

        post_url = self._base_url + '/' + FUZZY_CONSOLE_URL

        post_request(post_url, post_data, on_success, on_error)
